package cn.santi.controller;

import cn.santi.domain.Mycoupon;
import cn.santi.domain.SUser;
import cn.santi.emuns.R;
import cn.santi.service.MycouponService;
import cn.santi.service.OpenidCodeService;
import cn.santi.service.SUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static cn.santi.utli.DateTool.gettime;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private SUserService sUser;
    @Autowired
    private OpenidCodeService openidCodeService;
     @Autowired
     private MycouponService ms;
    @GetMapping("/openid")
    public R getOpenidByCode(String code) {//用户登录通过code获取openid
        // 假设这是你的获取openid的服务类
        System.out.println("code::" + code);
        String appId = "wx60afc1e2ff1ad77d";
        String secret = "629a834757b580e78c9a64a81ce82f40";
        String openid = openidCodeService.getOpenId(appId, secret, code);
        System.out.println("openid::" + openid);
        LambdaQueryWrapper<SUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SUser::getOpenId, openid);
        SUser one = sUser.getOne(wrapper);
        System.out.println("one:" + one);
        if (one != null) {
            return R.ok(one, "已有数据");
        } else {
            //添加
            SUser user = new SUser();
            user.setOpenId(openid);
            user.setBalance(BigDecimal.valueOf(0));
            user.setNickname("微信用户");
            sUser.save(user);
            SUser one1 = sUser.getOne(wrapper);
            System.out.println("one1:" + one1);
            //新用户送优惠卷
            Mycoupon mycoupon = new Mycoupon();
            mycoupon.setStatue("0");
            mycoupon.setUid(one1.getId());
            mycoupon.setDate(gettime());
            mycoupon.setCid(2);
            ms.save(mycoupon);
            return R.ok(one1, "刚添加");
        }
    }

    @PostMapping("/updUPhone")
    public R updateUser(Integer id, String entrydata,String iv) {
        System.out.println("id::" + id);
        System.out.println("entrydata::" + entrydata);
        System.out.println("iv::" + iv);
        SUser sUser1 = new SUser();
        sUser1.setId(id);
        sUser1.setPhone(entrydata);
        boolean b = sUser.updateById(sUser1);
        if (b){
            return R.ok("授权手机号成功");
        }else {
            return R.fail("授权手机号失败");
        }
    }

    @PostMapping("/getUser")
    public R getUser(Integer id) {
        System.out.println("进来了id:" + id);
        List<SUser> list = sUser.list();
        System.out.println("list::" + list);
        if (id == 6){
            return R.fail(-1,"");
        }else if (id == 7){
            return R.fail(-999,"hhh");
        }else return R.ok(list);
    }
    //修改用户头像路径
    @GetMapping("/updatauserbyurl")
    public R updatauserbyurl(int id ,String url){
        UpdateWrapper<SUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("avatarUrl",url);
        boolean b = sUser.update(wrapper);
        return R.ok(b);
    }
    //修改用户名称
    @GetMapping("/upadatauserbyname")
    public R upadatauserbyname(int id ,String nickname){
        UpdateWrapper<SUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("nickname",nickname);
        boolean b = sUser.update(wrapper);
        return R.ok(b);
    }
    //修改用户头像电话号码
    @GetMapping("/updatauserbyphone")
    public R updatauserbyphone(int id ,String phone){
        UpdateWrapper<SUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("phone",phone);
        boolean b = sUser.update(wrapper);
        return R.ok(b);
    }

}
